<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_check_for_downloads($order_id, $product_ID=0) {
    global $e107shop_db_name, $tp;
    $order_id   = $tp->toDB($order_id);
    $product_ID = (int)$product_ID;
    $sql2 = new db;
    $sql = new db;
    $sql3 = new db;
    //
    // set a marker at 0, will increase if there is a download
    //
    $i = 0;
    
    //
    // Query the order and check against the products
    //
    if($product_ID == 0)
    {
        $sql->db_Select($e107shop_db_name['orders'], "*", "order_number='".$order_id."'");
        while($row=$sql->db_Fetch())
        {
            $sql2->db_Select($e107shop_db_name['products'], "*", "ID=".$row["productID"]);
            while($row2=$sql2->db_Fetch())
            {
                if($row2["downloadable"]==1)
                {
                    $sql3->db_Insert($e107shop_db_name['downloads'], "0, '".$row["order_number"]."', '"
                    .$row2["download_file"]."', '".$row["theuser"]."', '', 0, 0, ".$row["productID"]);
                    $i++;
                }
            }
        }
    }
    else
    {
        $sql2->db_Select($e107shop_db_name['products'], "*", "ID=".$product_ID);
            while($row2=$sql2->db_Fetch())
            {
                if($row2["downloadable"]==1)
                {
                    $i++;
                }
            }
    }
    return $i;
}

function e107shop_check_details($order_number) {
    global $e107shop_db_name, $sql, $ns, $shop_settings, $tp;
    $order_number = $tp->toDB($order_number);
    if(!$sql->db_Select($e107shop_db_name['downloads'], "*", "order_number='"
    .$order_number."' AND USERNAME='".USERNAME."' Group by download_file")) {
        $ns->tablerender(lan_e107shop_download_title, lan_e107shop_download_error);
    } else {
        //
        // Start the display
        //
        $display = "<center><br />".lan_e107shop_download_files."<br /><br /><table class='fborder' width='95%'>";
        while($row=$sql->db_Fetch()) {
            //
            // Check whether the product is downloadable
            //
            
                $display.= "<tr>
                                <td>&nbsp;"
                                .e107shop_get_product_field($row["productID"], "product_name")."
                                </td>
                                <td class='forumheader3' style='text-align: right;'>
                                    <a href='get_download.php?p=".$row["productID"]."&o=".$order_number."'>".lan_global_download."&nbsp;</a>
                                </td>
                            </tr>";
            
        }
        $display.="</table><br />";

        $ns->tablerender($shop_settings["store_name"], $display);
    }
}
